package com.jiawa.train.business.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.jiawa.train.business.domain.ConfirmOrder;
import com.jiawa.train.business.domain.Req.ConfirmOrderDoReq;
import com.jiawa.train.business.service.ConfirmOrderService;
import com.jiawa.train.common.exception.BusinessException;
import com.jiawa.train.common.exception.BusinessExceptionEnum;
import com.jiawa.train.common.resp.CommonResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/confirm-order")
public class ConfirmOrderController {
    @Autowired
    private ConfirmOrderService confirmOrderService;


    @PostMapping("/do")
    @SentinelResource(value = "doConfirm",blockHandler = "doConfirmBlock")
    public CommonResp<Object> doConfirmOrder(@RequestBody ConfirmOrderDoReq confirmOrderDoReq) throws Exception {
        confirmOrderService.doConfirm(confirmOrderDoReq);
        return new CommonResp<>();
    }

    /**
     * 降级方法
     * @param confirmOrderDoReq
     * @param e
     */
    public CommonResp<Object> doConfirmBlock(ConfirmOrderDoReq confirmOrderDoReq, BlockException e){
      //throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_CODE_UNIQUE_ERROR);
        /*CommonResp<Object> commonResp = new CommonResp<>();
        commonResp.setSuccess(false);
        commonResp.setMessage(BusinessExceptionEnum.MEMBER_MOBILE_EXIST.getDesc());
        return commonResp;*/
        CommonResp<Object> commonResp = new CommonResp<>();
        commonResp.setSuccess(false);
        // 修改为明确的限流提示信息
        commonResp.setMessage("请求过于频繁，请稍后再试");
        return commonResp;
    }


}